#include "StdAfx.h"
#include "ArcBuilder.h"
#include "labeltools.h"
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <gp_Circ.hxx>
#include <gp_Pln.hxx>
#include <gp_Vec.hxx>
#include <GC_MakeArcOfCircle.hxx>
#include "VarList.h"
#include "ShapeInfoFactory.h"

REGISTERE_SHAPEINFO(CArcInfo, ARC_CRPE_GUID);
//////////////////////////////////////////////////////////////////////////
CArcInfo::CArcInfo():dAngle(0)
{
	m_szGUID = ARC_CRPE_GUID;
}
CArcInfo::~CArcInfo()
{
}
void CArcInfo::Reset()
{
	CCircleInfo::Reset();

	dAngle = 0;
}
void CArcInfo::GetParams(CVarList& varList, UINT nID)
{
	CCircleInfo::GetParams(varList, nID);
	varList.SetValue(START_POINT, ptStart);
	varList.SetValue(END_POINT, ptEnd);
	varList.SetValue(ANGLE, dAngle);

	varList.SetID(ARC_CRPE_GUID);
}

void CArcInfo::SetParams(const CVarList& varList, UINT nID)
{
	CCircleInfo::SetParams(varList, nID);

	CPointProxy point;
	varList.GetValue(START_POINT, point);
	ptStart = point;

	varList.GetValue(END_POINT, point);
	ptEnd = point;

	varList.GetValue(ANGLE, dAngle);

}

bool CArcInfo::GetFromLabel(TDF_Label& label)
{
	if(!CCircleInfo::GetFromLabel(label))
		return false;

	TDF_Label childLabel;
	if(!LabelTools::GetChild(label, START_POINT, ptStart, childLabel))
		return false;

	if(!LabelTools::GetChild(label, END_POINT, ptEnd, childLabel))
		return false;

	if(!LabelTools::GetChild(label, ANGLE, dAngle, childLabel))
		return false;

	return true;
}

void CArcInfo::AddToLabel(TDF_Label& label)
{
	CCircleInfo::AddToLabel(label);

	LabelTools::SetChild(label, START_POINT, ptStart);
	LabelTools::SetChild(label, END_POINT, ptEnd);
	LabelTools::SetChild(label, ANGLE, dAngle);
}

bool CArcInfo::ModifyLabel(TDF_Label& label, TFunction_Logbook& log)
{
	if(!CCircleInfo::ModifyLabel(label, log))
		return false;

	if(!LabelTools::UpdateChild(label, START_POINT, ptStart, log))
		return false;

	if(!LabelTools::UpdateChild(label, END_POINT, ptEnd, log))
		return false;

	if(!LabelTools::UpdateChild(label, ANGLE, dAngle, log))
		return false;

	return true;
}


bool CArcInfo::IsModified(TDF_Label& label, const TFunction_Logbook& log)
{
	if(CCircleInfo::IsModified(label, log))
		return true;

	if(log.IsModified(label.FindChild(START_POINT)))
		return true; 
	if(log.IsModified(label.FindChild(END_POINT)))
		return true; 
	if(log.IsModified(label.FindChild(ANGLE)))
		return true; 

	return false;
}
//////////////////////////////////////////////////////////////////////////

CArcBuilder::CArcBuilder(void)
{
}

CArcBuilder::~CArcBuilder(void)
{
}

bool CArcBuilder::Build(const CString& szCmdParam, TopoDS_Shape& shape)
{
	return false;
}

bool CArcBuilder::Build(CArcInfo* pShapeInfo, TopoDS_Shape& shape)
{
	try
	{
		gp_Ax2 aix(pShapeInfo->ptCenter, pShapeInfo->dirNormal);
		gp_Circ circle(aix, pShapeInfo->dRadius);
		GC_MakeArcOfCircle make(circle, pShapeInfo->ptStart, pShapeInfo->ptEnd, false);
		const Handle_Geom_TrimmedCurve  curve = make.Value();
		BRepBuilderAPI_MakeEdge builder(curve);
		shape = builder.Shape();
	}
	catch (...) 
	{
		return false;
	}

	return true;
}
